package com.example.com.skills_utf8;


import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

 
public class _41MainActivity extends FragmentActivity  {

	
	
	fragment41_0 mHome1Fra;
	fragment41_1 mHome2Fra;
//	Home3Fra mHome3Fra;
	
	
	private LinearLayout mTab_item_container;
	private FragmentManager mFM = null;

	
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_41);
        
        
        Button btn_fragment1 = (Button)findViewById(R.id.btn_fragment1);
        Button btn_fragment2 = (Button)findViewById(R.id.btn_fragment2);
        btn_fragment1.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				changeTo1();
			}
		});
        
        btn_fragment2.setOnClickListener( new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
				changeTo2();
			}
		});
        
        
        
        changeTo1();
      
        
    }
	
	
	/**
	 * 切换fragement
	 */
	private void changeTo1() {
		
		
		if (null == mFM)
			mFM = getSupportFragmentManager();
		FragmentTransaction ft = mFM.beginTransaction();

		hideAllFragment(ft);	
		
		if( mHome1Fra == null){
			mHome1Fra = new fragment41_0();    
			ft.add(R.id.content_container, mHome1Fra);
		}
		else{
			ft.show(mHome1Fra);
		}
		ft.commit();
		
	//	setHandler(mHome1Fra.MyHandler);
	//	handler.sendEmptyMessage(0);
	}
	
	private void changeTo2() {

		
		
		if (null == mFM)
			mFM = getSupportFragmentManager();
		FragmentTransaction ft = mFM.beginTransaction();
		
		hideAllFragment(ft);
		
		if( mHome2Fra == null){
			mHome2Fra = new fragment41_1();    
			ft.add(R.id.content_container, mHome2Fra);
		}
		else{
			ft.show(mHome2Fra);
		}
		ft.commit();
		
	//	setHandler(mHome2Fra.MyHandler);
	//	handler.sendEmptyMessage(0);
	}
	
	/*
	private void changeTo3() {
		
		
		if (null == mFM)
			mFM = getSupportFragmentManager();
		FragmentTransaction ft = mFM.beginTransaction();
		
		hideAllFragment(ft);
		
		if( mHome3Fra == null){
			mHome3Fra = new Home3Fra();    
			ft.add(R.id.content_container, mHome3Fra);
		}
		else{
			ft.show(mHome3Fra);
		}
		ft.commit();
		
		setHandler(mHome3Fra.MyHandler);
		handler.sendEmptyMessage(0);
	}
	*/
	
	
	void hideAllFragment(FragmentTransaction transaction){
		if(mHome1Fra != null){
			transaction.hide(mHome1Fra);
		}
		
		if(mHome2Fra != null){
			transaction.hide(mHome2Fra);
		}
		
		/*
		if(mHome3Fra != null){
			transaction.hide(mHome3Fra);
		}
		*/
		
	}
	
	
	
	
	/*	  this.registerForContextMenu( 还要给个listview );  
	//http://www.cnblogs.com/codingmyworld/archive/2011/08/27/2155521.html
	// 生成上下文菜单
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		super.onCreateContextMenu(menu, v, menuInfo);
		menu.setHeaderTitle("操作");
		menu.add(0, 0, ContextMenu.NONE, "左");
		menu.add(0, 1, ContextMenu.NONE, "右");
		
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
		
		float delta_x = 0;
		float delta_y = 0;
		switch( item.getItemId() ){
		case 0:
			delta_x += 1;
			break;
		case 1:
			delta_y += 1;
			break;
		}
		
		params.leftMargin = params.leftMargin + (int)delta_x; 
		params.topMargin = params.topMargin + (int)delta_y;
		params.rightMargin = params.rightMargin + (int)delta_x;
		params.bottomMargin = params.bottomMargin + (int)delta_y;
		layout_dest.setLayoutParams(params);
		
		
		return super.onContextItemSelected(item);
	}
	*/
	
	
	
	


	

}


